package cn.mw.cmdb.entity;

import cn.hutool.extra.spring.SpringUtil;
import cn.mw.cmdb.param.MwRoomLayoutIntegration;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.InstanceServiceImpl;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import com.google.common.base.Strings;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.mongodb.core.query.Criteria;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.mw.cmdb.service.impl.InstanceViewImpl.idKey;
import static cn.mw.cmdb.view.InstanceListViewConverter.dataListMap;

public class PropertyTypeLayoutRoom extends PropertyType<PropertyTypeLayoutRoom, Object> {

    public static final String ID = "layoutRoom";
    private static Map<String, Map> finalMap = new HashMap();

    public PropertyTypeLayoutRoom() {
        setId(ID);
        setName("机房布局");
        setSortNum(20);
    }

    @Override
    public Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        return null;
    }

    @Override
    public CriteriaOpsType getDefaultCriteriaOpsType() {
        return null;
    }

    @Override
    public PropertyTypeLayoutRoom newObj() {
        return null;
    }

    @Override
    public Object lookValue(Object val, Map<String, Object> context) {
        Map ms = new HashMap();
        if (context != null && context.get(dataListMap) != null) {
            Object obj = context.get(dataListMap);
            if (obj instanceof Map) {
                Map mp = (Map) obj;
                String id = strValueConvert(mp.get(idKey));
                if (!Strings.isNullOrEmpty(id)) {
                    CmdbServiceManage cmdbServiceManage = SpringUtil.getBean(CmdbServiceManage.class);
                    InstanceServiceImpl service = (InstanceServiceImpl) cmdbServiceManage.getModelService(InstanceServiceImpl.ID);
                    try {
                        List<MwRoomLayoutIntegration> roomLayoutIntegration = service.getRoomLayoutIntegration(Arrays.asList(id));
                        if (CollectionUtils.isNotEmpty(roomLayoutIntegration)) {
                            return roomLayoutIntegration.get(0).getRoomLayoutData();
                        }
                    } catch (Exception e) {
                        return val;
                    }
                }
            }
        }
        return val;
    }


    @Override
    public Object readValue(Object val, Map<String, Object> context) {
        Object obj = lookValue(val, context);
        return obj;
    }

    @Override
    public Object selValue(Object val, Map<String, Object> context) {
        Object obj = lookValue(val, context);
        return obj;
    }

    /**
     * 导出数据转换
     *
     * @param val
     * @param context
     * @return
     */
    @Override
//    @Override
    public Object exportValue(Object val, Map<String, Object> context) {
        Map map = new HashMap();

        return map;
    }


    @Override
    public void refresh() {
        finalMap.clear();
    }

}
